package com.ruoyi.common.utils.wxmessage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 发送订阅消息
 *
 * @author LCL
 */
public class SendWxMessage {

	public static final String appId = "wxc0574270723e1af8";
	public static final String appSecret = "095f076a6c63cede2a6c38e63990620e";

	/**
	 * 订单发货通知
	 *
	 * @param openid：小程序用户openid
	 * @return 小程序服务通知
	 */
	public static String pushOrderDeliveryNotice(String openid, String orderNo) {
		RestTemplate restTemplate = new RestTemplate();
		//这里简单起见我们每次都获取最新的access_token（应该在access_token快过期时再重新获取）
		String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();
		//拼接推送的模版
		WxMssVo wxMssVo = new WxMssVo();
		wxMssVo.setTouser(openid);//用户的openid（要发送给那个用户，通常这里应该动态传进来的）
		wxMssVo.setTemplate_id("j6nzccIWUfGW7yeqGHOFxzREaF_e45ipTnu2d0zR2aM");//订阅消息模板id
		wxMssVo.setPage("pages/index/index");

		Map<String, TemplateData> m = new HashMap<>(3);
		//订单编号 {{character_string1.DATA}}
		//物品名称 {{thing7.DATA}}
		//订单状态 {{phrase2.DATA}}
		//发货时间 {{date6.DATA}}
		//备注 {{thing9.DATA}}
		m.put("character_string1", new TemplateData(orderNo));
		m.put("thing7", new TemplateData("羽禽链商品"));
		m.put("phrase2", new TemplateData("已发货"));
		m.put("date6", new TemplateData(DateUtils.getTime()));
		m.put("thing9", new TemplateData("您购买的商品已发货，请注意查收"));

		wxMssVo.setData(m);
		ResponseEntity<String> responseEntity =
				restTemplate.postForEntity(url, wxMssVo, String.class);
		return responseEntity.getBody();
	}

	public static String getAccessToken() {
		RestTemplate restTemplate = new RestTemplate();
		Map<String, String> params = new HashMap<>();
		params.put("APPID", appId);  //小程序APP_ID
		params.put("APPSECRET", appSecret);  //小程序APP_SECRET
		ResponseEntity<String> responseEntity = restTemplate.getForEntity(
				"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}", String.class, params);
		String body = responseEntity.getBody();
		JSONObject object = JSON.parseObject(body);
		String Access_Token = object.getString("access_token");
		String expires_in = object.getString("expires_in");
		System.out.println("有效时长expires_in：" + expires_in);
		return Access_Token;
	}

	/**
	 * 会员付费成功通知
	 *
	 * @param openid：小程序用户openid
	 * @return 小程序服务通知
	 */
	public static String pushMemberPayment(String openid, String money) {
		RestTemplate restTemplate = new RestTemplate();
		//这里简单起见我们每次都获取最新的access_token（应该在access_token快过期时再重新获取）
		String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getAccessToken();
		//拼接推送的模版
		WxMssVo wxMssVo = new WxMssVo();
		wxMssVo.setTouser(openid);//用户的openid（要发送给那个用户，通常这里应该动态传进来的）
		wxMssVo.setTemplate_id("GkO4KYjDmDYho_yLvhkV02sS2ax9kh6Rrf6tjS4xUfQ");//订阅消息模板id
		wxMssVo.setPage("pages/index/index");

		Map<String, TemplateData> m = new HashMap<>(3);
		//物品名称 {{thing1.DATA}}
		//订单状态 {{phrase3.DATA}}
		//支付金额 {{amount6.DATA}}
		//支付时间 {{date7.DATA}}
		m.put("thing1", new TemplateData("会员支付"));
		m.put("phrase3", new TemplateData("已支付"));
		m.put("amount6", new TemplateData(money));
		m.put("date7", new TemplateData(DateUtils.getTime()));

		wxMssVo.setData(m);
		ResponseEntity<String> responseEntity =
				restTemplate.postForEntity(url, wxMssVo, String.class);
		return responseEntity.getBody();
	}

}
